package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.ProtectType;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITbCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("扩展表管理")
@RestController
@RequestMapping("/system/tbCompany")
public class TbCompanyController extends BaseController {

    @Autowired
    private ITbCompanyService tbCompanyService;
    @Autowired
    private ISysUserService userService;

    @PostMapping("/save")
    public AjaxResult save(@RequestBody Map<String, Object> data) {
        Long uid = getUserId();
        String uname = getUsername();
        data.put("create_id", uid);
        data.put("create_by", uname);
        data.put("create_time", new Date());
        data.put("status", ProtectType.wbh.getLabel());
        Map rs = tbCompanyService.save(data);

        return success(rs);
    }

    @PostMapping("/saveCommon")
    public AjaxResult saveCommon(@RequestBody Map<String, Object> data) {
        Long uid = getUserId();
        String uname = getUsername();
        data.put("create_id", uid);
        data.put("create_by", uname);
        data.put("create_time", new Date());
        Map rs = tbCompanyService.save(data);

        return success(rs);
    }

    @PostMapping("/update")
    public AjaxResult update (@RequestBody Map<String, Object> data) {
        Long uid = getUserId();
        String uname = getUsername();
        data.put("update_id", uid);
        data.put("update_by", uname);
        data.put("update_time", new Date());
        Map rs = tbCompanyService.update(data);

        return success(rs);
    }

    @ApiOperation("保护")
    @PostMapping("/protect")
    public AjaxResult protect(@RequestBody Map<String, Object> data) {
        Long uid = getUserId();
        String uname = getNickName();

        // 添加保护人、时间、状态信息
        data.put("bhrid", uid);
        data.put("bhr", uname);
        data.put("bhsj", new Date());
        data.put("removeNote", null);
        data.put("status", ProtectType.bhz.getLabel());

        Map rs = tbCompanyService.update(data);
        return success();
    }



    @GetMapping("/info")
    public AjaxResult info(Long id, String tableName) {
        return success(tbCompanyService.getById(id, tableName));
    }

    @ApiOperation("分页列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String, Object> params, Long tableId) {
        startPage();
        return getDataTable(tbCompanyService.list(params, tableId));
    }

    @PostMapping("/listAll")
    public AjaxResult listAll(@RequestBody Map<String, Object> params, Long tableId) {
        List list = tbCompanyService.list(params, tableId);
        return success(list);
    }

    @ApiOperation("保护列表")
    @PostMapping("/selectProtected")
    public TableDataInfo selectProtected(@RequestBody Map<String, Object> params, Long tableId) {
        startPage();
        SysUser currUser = super.getLoginUser().getUser();
        if(!currUser.isAdmin()) {
            List<Long> userIds = userService.querySubUserIds(currUser);
            params.put("bhrid__FILTER__in", userIds);
        }

        if(StringUtils.isNullOrEmpty(params.get("status"))) {
            params.put("status__FILTER__NE", ProtectType.wbh.getLabel());
        }

        return getDataTable(tbCompanyService.list(params, tableId));
    }


    @Log(title = "字段描述", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long[] ids, Long tableId) {
        return toAjax(tbCompanyService.delete(ids, tableId));
    }


}
